package com.hzhk.happyrent.ui.fragment.home;

import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.rent.BaseBean;
import com.hzhk.happyrent.bean.rent.ProductListNewsBean;
import com.hzhk.happyrent.bean.rent.RentHeaderPagerBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyResponseCallback;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.adapter.rent.RentLoadAdapter;
import com.hzhk.happyrent.ui.fragment.BaseFragment;
import com.hzhk.happyrent.ui.view.listview.MaiTianListview;
import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxResultCallback;
import com.tamic.novate.callback.RxStringCallback;
import com.tamic.novate.response.NovateResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.ResponseBody;

/**
 * User: hzhk(1097282555@qq.com)
 * Date: 2017/11/21
 * Time: 18:00
 * progject: HappyRent
 * Describe： ${Describe}
 */
public class RentFragment extends BaseFragment implements  MaiTianListview.OnMaiTianRefreshListener{

    @BindView(R.id.lv_fragment_rent)
    MaiTianListview lvFragmentRent;
    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    private RentLoadAdapter rentLoadAdapter;
    private View mFragmentView;
    private int page=1;

    private List<BaseBean> baseBeans= MyPerference.getInstance().getDataList(Constants.HOME_SAVE_LOCAL,BaseBean.class);
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void initData() {
        HttpManager.getInstance().getRent(new MyResponseCallback<BaseBean>(getActivity()) {
            @Override
            public void onError(Object tag, Throwable e) {
                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                lvFragmentRent.stopRefresh();
            }

            @Override
            public void onStart(Object tag) {
                super.onStart(tag);
                baseBeans.clear();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, int code, String message, BaseBean response) {
                BaseBean baseBean=response;
                baseBeans.add(response);
            }

            @Override
            public void onCompleted(Object tag) {
                super.onCompleted(tag);
                MyPerference.getInstance().setDataList(Constants.HOME_SAVE_LOCAL,baseBeans);
                rentLoadAdapter.setList(baseBeans);
                lvFragmentRent.stopRefresh();
            }
        });

    }

    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_rent, null);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null == mFragmentView) {
            mFragmentView = super.onCreateView(inflater, container, savedInstanceState);
            initView();
            initData();
        }
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void initView(){
        activityTitlebarCenter.setText(getString(R.string.rent_title));
        activityTitlebarLeft.setVisibility(View.GONE);
        activityTitlebarRight.setVisibility(View.GONE);
        lvFragmentRent.setOnMaiTianRefreshListener(this);
        rentLoadAdapter = new RentLoadAdapter(getContext());
        rentLoadAdapter.setList(baseBeans);
        lvFragmentRent.setAdapter(rentLoadAdapter);
    }
    @Override
    public void onDestroyView(){
        super.onDestroyView();
        if (null != mFragmentView) {
            ((ViewGroup) mFragmentView.getParent()).removeView(mFragmentView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
//        rentLoadAdapter=new RentLoadAdapter(getContext());
        lvFragmentRent.setFooterText("加载更多");
        lvFragmentRent.setFooterEnable(true);
        page=1;
        initData();
    }

    @Override
    public void onLoadMore() {
        page++;
        loadMoreData();
    }
    private void loadMoreData(){
        Map<String,Object> params=new HashMap<>();
        params.put("action","2");
        params.put("goods_type","1");
        params.put(Constants.PAGE,page);
        params.put(Constants.LIMIT,Constants.PAGE_CARD_SIZE);
        HttpManager.getInstance().get(API.GOODS_LIST, params, new RxStringCallback() {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                ProductListNewsBean productListNewsBean=new Gson().fromJson(response,ProductListNewsBean.class);
                if(productListNewsBean.getData()!=null){
                    if(baseBeans.size()==4){
                        if(!ListUtils.isEmpty(productListNewsBean.getData().getGoods())){
                            ((ProductListNewsBean)baseBeans.get(3)).getData().getGoods().addAll(productListNewsBean.getData().getGoods());
                            rentLoadAdapter.setList(baseBeans);
                        }
                        if(productListNewsBean.getData().getGoods().size()<Constants.PAGE_CARD_SIZE){
                            lvFragmentRent.allLoadFinish();
                        }
                    }
                }
            }

            @Override
            public void onCompleted(Object tag) {
                super.onCompleted(tag);
                lvFragmentRent.stop();
            }
        });
    }

}
